package com.vogue.supplymall.match.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.vogue.supplymall.account.domain.model.BaseCode;
import com.vogue.supplymall.account.domain.service.BaseCategoryService;
import com.vogue.supplymall.account.domain.service.BaseCodeService;
import com.vogue.supplymall.admin.business.domain.service.AdminOrganService;
import com.vogue.supplymall.common.constant.ConstantBean;
import com.vogue.supplymall.common.domain.tool.BeanToolsUtils;
import com.vogue.supplymall.common.domain.tool.GsonUtils;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.match.domain.dto.BaseDTO;
import com.vogue.supplymall.match.domain.model.ServiceResult;
import com.vogue.supplymall.match.domain.service.ColValidateService;
import com.vogue.supplymall.shop.domain.model.Administrativearea;
import com.vogue.supplymall.shop.domain.service.AdministrativeareaService;

@RestController
public class CommonInfoOfSupplymallController extends BaseController{
	@Autowired
	private BaseCategoryService baseCategoryService;
	@Autowired
	private AdministrativeareaService administrativeareaService;
	@Autowired
	private ColValidateService colValidateService;
	@Autowired
	private MessageSourceUtils msu;
	@Autowired
	private AdminOrganService adminOrganService;
	@Autowired
	private BaseCodeService baseCodeService;
	
	/**
	 * 得到商城所有的商品类别</br>
	 * tyc</br>
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/ns/v1/match/getAllCategory",method=RequestMethod.POST)
	public void getAllCategory(HttpServletRequest request,HttpServletResponse response) {
		boolean resultFlag=true;
		String message=msu.getMessage("global.system.success",null);	//成功
		List<Map<String, Object>> errList=new ArrayList<>();
		List<Map<String, Object>> categoryList=new ArrayList<Map<String,Object>>();
		
		//首先进行验证
		try {
			ServletInputStream sis=request.getInputStream();
			String datas=BeanToolsUtils.streamToString(sis);
			BaseDTO<String> baseDTO=colValidateService.setValueTobaseDTO(datas);
			errList=colValidateService.safetyValidate(baseDTO,true, "goodsCategory");
			
		} catch (Exception e) {
			resultFlag=false;
			message=msu.getMessage("match.getAllCategory.getParamFail", null); //参数解析失败
			e.printStackTrace();
		}
	
		if(errList.size()==0) {
			try {
				categoryList = baseCategoryService.getAllCategory();
			} catch (Exception e1) {
				resultFlag=false;
				message=msu.getMessage("match.getAllCategory.getCategoryFail", null); //取商品类别失败
				e1.printStackTrace();
			}
		}else {
			resultFlag=false;
			message=msu.getMessage("match.getAllCategory.getCategoryFail", null);
			for(Map<String, Object> map:errList) {
				List<String> list=(List<String>) map.get("errInfo");
				for(String errMsg:list) {
					message+=","+errMsg+";";
				}
				break;
			}
		}
		
		ServiceResult<List<Map<String, Object>>> sr=new ServiceResult<List<Map<String,Object>>>(resultFlag, message, categoryList);
		
		//返回数据
		try {
			GsonUtils.ajaxJson(response, GsonUtils.GSON_EXPOSE_DATEFORMATE_HMS.toJson(sr));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 得到商城的省市区域信息
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/ns/v1/match/getAllAdministrativeInfo",method=RequestMethod.POST)
	public void getAllAdministrativeInfo(HttpServletRequest request,HttpServletResponse response) {
		boolean resultFlag=true;
		String message="成功";
		List<Map<String, Object>> errList=new ArrayList<>();
		List<Administrativearea> asa=new ArrayList<>();
		
		//首先进行验证
		try {
			ServletInputStream sis=request.getInputStream();
			String datas=BeanToolsUtils.streamToString(sis);
			BaseDTO<String> baseDTO=colValidateService.setValueTobaseDTO(datas);
			errList=colValidateService.safetyValidate(baseDTO, true,"administrative");
			
		} catch (Exception e) {
			resultFlag=false;
			message=msu.getMessage("match.getAllCategory.getParamFail", null); //参数解析失败
			e.printStackTrace();
		}
		
		if(errList.size()==0) {
			asa=administrativeareaService.findAllAreaOfMinLevel(ConstantBean.AREA_LEVEL_3);
			
			if(asa.size()==0||asa.get(0)==null) {
				resultFlag=false;
				message="同步行政区域信息失败";
			}
		}else {
			resultFlag=false;
			message=msu.getMessage("match.getAdministrative.getAdministrativeFail", null);
			for(Map<String, Object> map:errList) {
				List<String> list=(List<String>) map.get("errInfo");
				for(String errMsg:list) {
					message+=","+errMsg+";";
				}
				break;
			}
		}
		
		ServiceResult<List<Administrativearea>> sr=new ServiceResult<List<Administrativearea>>(resultFlag, message, asa);
		
		try {
			GsonUtils.ajaxJson(response, GsonUtils.GSON_EXPOSE_DATEFORMATE_HMS.toJson(sr));
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 得到马甲信息
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/ns/v1/match/getAdminDict",method=RequestMethod.POST)
	public void getAdminDict(HttpServletRequest request,HttpServletResponse response) {
		boolean resultFlag=true;
		String message="成功";
		List<Map<String, Object>> errList=new ArrayList<>();
		List<Map<String, Object>> resultList=new ArrayList<>();
		BaseDTO<String> baseDTO=null;
		
		//首先进行验证
		try {
			ServletInputStream sis=request.getInputStream();
			String datas=BeanToolsUtils.streamToString(sis);
			baseDTO=colValidateService.setValueTobaseDTO(datas);
			errList=colValidateService.safetyValidate(baseDTO,true, "adminDict");
			
		} catch (Exception e) {
			resultFlag=false;
			message=msu.getMessage("match.getAllCategory.getParamFail", null); //参数解析失败
			e.printStackTrace();
		}
		
		if(errList.size()==0) {
			resultList=adminOrganService.getAdminDict();
		}else {
			resultFlag=false;
			message=msu.getMessage("match.getPlatFormFail", null);
			for(Map<String, Object> map:errList) {
				List<String> list=(List<String>) map.get("errInfo");
				for(String errMsg:list) {
					message+=","+errMsg+";";
				}
				break;
			}
		}
		
		ServiceResult<List<Map<String, Object>>> sr=new ServiceResult<List<Map<String, Object>>>(resultFlag, message, resultList);
		
		try {
			GsonUtils.ajaxJson(response, GsonUtils.GSON_EXPOSE_DATEFORMATE_HMS.toJson(sr));
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * 得到企业入驻的平台代码
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/ns/v1/match/getAllPlatForm",method=RequestMethod.POST)
	public String getAllPlatForm(HttpServletRequest request,HttpServletResponse response) {
		boolean resultFlag=true;
		String message="成功";
		List<Map<String, Object>> errList=new ArrayList<>();
		List<Map<String, Object>> resultList=new ArrayList<>();
		BaseDTO<String> baseDTO=null;
		Map<String, Object> resultMap=new HashMap<>();
		
		//首先进行验证
		try {
			ServletInputStream sis=request.getInputStream();
			String datas=BeanToolsUtils.streamToString(sis);
			baseDTO=colValidateService.setValueTobaseDTO(datas);
			errList=colValidateService.safetyValidate(baseDTO,true, "platForm");
			
		} catch (Exception e) {
			resultFlag=false;
			message=msu.getMessage("match.getAllCategory.getParamFail", null); //参数解析失败
			e.printStackTrace();
		}
		
		if(errList.size()==0) {
			//将注册平台的马甲号放在第一位
			resultList=adminOrganService.getPlatFormByST(baseDTO.getRegistercode(), baseDTO.getRegistertel());
			
			if(resultList.size()==0||resultList.get(0)==null) {
				Map<String, Object> map=new HashMap<>();
				
				Long sysCodeId =baseCodeService.checkSysCodeValidity(baseDTO.getRegistercode());
				BaseCode bc=baseCodeService.get(sysCodeId);
				int channel=bc.getChannel();
				map.put("platform", channel);
				resultList.add(map);
			}
		}else {
			resultFlag=false;
			message=msu.getMessage("match.getPlatFormFail", null);
			for(Map<String, Object> map:errList) {
				List<String> list=(List<String>) map.get("errInfo");
				for(String errMsg:list) {
					message+=","+errMsg+";";
				}
				break;
			}
		}
		
		//返回数据
		resultMap.put("result", resultFlag);
		resultMap.put("message", message);
		resultMap.put("details", errList);
		resultMap.put("data", resultList);
		String json=new Gson().toJson(resultMap);
		
		return json;
		
	}
	

}
